package core.comparators;
import core.Book;
import java.util.Comparator;


/**
 *
 * @author Administrator
 */
public class BookComparator {

    public static enum BOOKCOMPARATORTYPE
    {
        ID,
        ISBN10,
        ISBN13,
        TITLE,
        AUTHOR,
        DESCRIPTION,
        LINK,
        PRICE
    };
    public Comparator getComparator(BOOKCOMPARATORTYPE type, boolean ascending)
    {
        switch(type)
        {

            case ISBN10:
                return new CompareISBN10(ascending);
            case ISBN13:
                return new CompareISBN13(ascending);
            case TITLE:
                return new CompareTitle(ascending);
            case AUTHOR:
                return new CompareAuthor(ascending);
            case DESCRIPTION:
                return new CompareDescription(ascending);
            case LINK:
                return new CompareLink(ascending);
            case PRICE:
                return new ComparePrice(ascending);
            default:
            case ID:
                return new CompareId(ascending);
            
        }
        
    }

    private class CompareISBN10 implements Comparator
    {
        boolean ascending;
        CompareISBN10(boolean ascending)
        {
            this.ascending = ascending;
        }
        public int compare(Object o1, Object o2) {
                Book b1 = (Book) o1;
                Book b2 = (Book) o2;

            return ascending ?
                b1.getIsbn10().compareTo(b2.getIsbn10()) :
                b2.getIsbn10().compareTo(b1.getIsbn10());
        }
    }

        private class CompareISBN13 implements Comparator
    {
        boolean ascending;
        CompareISBN13(boolean ascending)
        {
            this.ascending = ascending;
        }
        public int compare(Object o1, Object o2) {
                Book b1 = (Book) o1;
                Book b2 = (Book) o2;

            return ascending ?
                b1.getIsbn13().compareTo(b2.getIsbn13()) :
                b2.getIsbn13().compareTo(b1.getIsbn13());
        }
    }
            private class CompareTitle implements Comparator
    {
        boolean ascending;
        CompareTitle(boolean ascending)
        {
            this.ascending = ascending;
        }
        public int compare(Object o1, Object o2) {
                Book b1 = (Book) o1;
                Book b2 = (Book) o2;

            return ascending ?
                b1.getTitle().compareTo(b2.getTitle()) :
                b2.getTitle().compareTo(b1.getTitle());
        }
    }
    private class CompareAuthor implements Comparator
    {
        boolean ascending;
        CompareAuthor(boolean ascending)
        {
            this.ascending = ascending;
        }
        public int compare(Object o1, Object o2) {
                Book b1 = (Book) o1;
                Book b2 = (Book) o2;

            return ascending ?
                b1.getAuthor().compareTo(b2.getAuthor()) :
                b2.getAuthor().compareTo(b1.getAuthor());
        }
    }
        private class CompareDescription implements Comparator
    {
        boolean ascending;
        CompareDescription(boolean ascending)
        {
            this.ascending = ascending;
        }
        public int compare(Object o1, Object o2) {
                Book b1 = (Book) o1;
                Book b2 = (Book) o2;

            return ascending ?
                b1.getDescription().compareTo(b2.getDescription()) :
                b2.getDescription().compareTo(b1.getDescription());
        }
    }

    private class CompareLink implements Comparator
    {
        boolean ascending;
        CompareLink(boolean ascending)
        {
            this.ascending = ascending;
        }
        public int compare(Object o1, Object o2) {
                Book b1 = (Book) o1;
                Book b2 = (Book) o2;

            return ascending ?
                b1.getLink().compareTo(b2.getLink()) :
                b2.getLink().compareTo(b1.getLink());
        }
    }
    private class ComparePrice implements Comparator
    {
        boolean ascending;
        ComparePrice(boolean ascending)
        {
            this.ascending = ascending;
        }
        public int compare(Object o1, Object o2) {
                Book b1 = (Book) o1;
                Book b2 = (Book) o2;

                Long v1 = new Long(b1.getPrice());
                Long v2 = new Long(b2.getPrice());

            return ascending ?
                v1.compareTo(v2) :
                v2.compareTo(v1);
        }
    }
    private class CompareId implements Comparator
    {
        boolean ascending;
        CompareId(boolean ascending)
        {
            this.ascending = ascending;
        }
        public int compare(Object o1, Object o2) {
                Book b1 = (Book) o1;
                Book b2 = (Book) o2;

                Long v1 = new Long(b1.getId());
                Long v2 = new Long(b2.getId());

            return ascending ?
                v1.compareTo(v2) :
                v2.compareTo(v1);
        }
    }

}
